package com.zyl2015;

public abstract class AbstractCollection<E> implements Collection<E> {

	public abstract Iterator<E> iterator(); 

	public abstract int size();
	
	public boolean isEmpty(){
		return size()==0;
	}
	
	@Override
	public boolean remove(Object o) {
		Iterator<E> it=iterator();
		while(it.hasNext()){
			if(o.equals(it.next())){
				it.remove();
				return true;
			}
		}
		return false;
	}
	
	@Override
	public boolean add(E e) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void clear() {
		Iterator<E> it=iterator();
		while(it.hasNext()){
			it.next();
			it.remove();
		}
		
	}

	@Override
	public boolean contains(Object o) {
		Iterator<E> it=iterator();
		while(it.hasNext()){
			if(o.equals(it.next()))
				return true;
		}
		return false;
	}
	
	@Override
	public String toString(){
		StringBuilder sb=new StringBuilder("[");
		Iterator<E> it=iterator();
		while(it.hasNext()){
			sb.append(it.next().toString());
			if(it.hasNext())
				sb.append(",");
		}
		sb.append("]");
		return sb.toString();
	}

	

}
